<?php
require "common.php";
$common=new Common();
$error_arr=[];
$query_arr=[];
if ($_POST){
 //   var_dump($_POST);die;
    if (empty($_POST["form_token"])&& $_POST["form_token"]!=$_SESSION['form_token']) Common::rf("token错误!");
    $sql="SELECT * FROM `lunwen_dict`";
    $query=mysqli_query($common->startMysql(),$sql);
    $info_arr=mysqli_fetch_all($query,MYSQLI_ASSOC);
    $blokType=[];
    $jump_arr=['mainText','stuid'];
    mysqli_autocommit($common->startMysql(),FALSE);
    foreach ($info_arr as $info_arr_v){
       if(in_array($info_arr_v["blokType"],$jump_arr)) continue;
        if (!empty($_POST[$info_arr_v["blokType"]])){
            $sql="SELECT * FROM lunwen_yuanfile WHERE user_info_id='".$_SESSION['id']."' and blokType='".$info_arr_v["blokType"]."';";
            $query=mysqli_query($common->startMysql(),$sql);
            $info_arr=mysqli_fetch_array($query,MYSQLI_ASSOC);
            if (empty($info_arr['正文内容'])?"":$info_arr['正文内容']==$_POST[$info_arr_v["blokType"]]) continue;
            $_POST[$info_arr_v["blokType"]]=$common->sqlSafeString($_POST[$info_arr_v["blokType"]]);
            if ($info_arr){
                $sql="UPDATE `lunwen_yuanfile` SET `正文内容`='".$_POST[$info_arr_v["blokType"]]."' WHERE (`id`='".$info_arr['id']."')";
                $query=mysqli_query($common->startMysql(),$sql);
                $query_arr[]=$query;
                if (!$query){
                    $error_arr[]=$info_arr_v["blokType"];
                }
            }else{
                $sql="INSERT INTO `lunwen_yuanfile` (`user_info_id`, `stuid`, `d0`, `d1`, `d2`, `d3`, `blokType`, `区块类型`, `正文标题`, `正文内容`) VALUES ('".$_SESSION["id"]."', '".$_SESSION['stuid']."', '0', '0', '0', '0', '".$info_arr_v["blokType"]."', '".$info_arr_v["blokType_cn"]."', '', '".$_POST[$info_arr_v["blokType"]]."')";
                $query=mysqli_query($common->startMysql(),$sql);
                $query_arr[]=$query;
                if (!$query){
                    $error_arr[]=$info_arr_v["blokType"];
                }else{
                    $sql="UPDATE `lunwen_user_info` SET `".$info_arr_v["blokType"]."`='".mysqli_insert_id($common->startMysql())."' WHERE (`id`='".$_SESSION["id"]."')";
                    $query=mysqli_query($common->startMysql(),$sql);
                    $query_arr[]=$query;
                    if (!$query){
                        $error_arr[]=$info_arr_v["blokType"]."_Update";
                    }
                }

            }
        }
    }

    $is_commit=true;
    foreach ($query_arr as $query_arr_v){
        if ($query_arr_v==false){
            $is_commit=false;
        }
    }

    if($is_commit){
        mysqli_commit($common->startMysql());
        Common::rs("提交成功");
    }else{
        mysqli_rollback($common->startMysql());
        $msg=implode(",",$query_arr);
        Common::rf("失败！涉及:".$msg);
    }



}